package wildlife;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;


public class showposts {
	String post;
	String username;
	int id;
	String date;
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPost() {
		return post;
	}
	public void setPost(String post) {
		this.post = post;
	}
	private ArrayList<showposts> posts = new ArrayList<showposts>();
	public ArrayList<showposts> getPosts() {
		return posts;
	}
	public void setPosts(ArrayList<showposts> posts) {
		this.posts = posts;
	}
	
	public Boolean checkadmin()
	{
		if(username.equals("admin"))
		{return true;}
		else
			{return false;}
	}
	
	public String execute() throws SQLException
	{
		ResultSet rs;
		String query;
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		String username=session.getAttribute("username")+"";
		if(username.equals("admin"))
		{
			query="select id,notification,username,date from notifications where approved=0";
		}
		else
		{
			query="select id,notification,username,date from notifications where username='"+username+"'";
		}
		Connect c=new Connect();
		rs=c.getResult(query);
		while(rs.next())
		{
			showposts sp=new showposts();
			sp.setPost(rs.getString("notification"));
			sp.setUsername(rs.getString("username"));
			sp.setId(rs.getInt("id"));
			sp.setDate(rs.getTimestamp("date")+"");
			System.out.println("post : "+rs.getInt("id")+"  "+rs.getString("notification")+"  "+rs.getTimestamp("date"));
			posts.add(sp);
		}
		return "success";
	}
}
